﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Web;
using System.Configuration;

namespace VLog
{
    /// <summary>
    /// VLog 基类
    /// </summary>
    public abstract class VLogBase : IVLog
    {
        #region Protected Properties
        /// <summary>
        /// 获取执行路径
        /// </summary>
        /// <returns>返回 执行路径</returns>
        protected string GetSenders()
        {
            StringBuilder sb = new StringBuilder();
            StackFrame[] frames = new StackTrace(true).GetFrames();
            for (int index = 2; index < frames.Length; index++)
            {
                if (frames[index].GetMethod().ReflectedType == null)
                {
                    sb.AppendFormat("［systemfuntion:{0}］－＞", frames[index].GetMethod().Name);
                }
                else
                {
                    sb.AppendFormat("［{0}］－＞", frames[index].GetMethod().ReflectedType.Name);
                }
            }
            //return sb.ToString(0, sb.Length - 2);
            return string.Empty;
        }

        /// <summary>
        /// 得到当现异常的URL
        /// 子类可以根据自己的逻辑去复写本属性
        /// </summary>
        protected virtual string RequestReffer { get { return HttpContext.Current.Request.Url.AbsoluteUri; } }

        /// <summary>
        /// 得到当前异常URL的请求方式（POST，GET或HEAD）
        /// 子类可以根据自己的逻辑去复写本属性
        /// </summary>
        protected virtual string HttpMethod { get { return HttpContext.Current.Request.HttpMethod; } }

        /// <summary>
        /// 写日志地址 或是 DBConnectionStr
        /// </summary>
        protected string Path { get; set; }
        #endregion

        #region Protected Methods
        /// <summary>
        /// 写日志,子类必须去实现这个方法
        /// </summary>
        /// <param name="text">内容</param>
        /// <returns>返回是否成功</returns>
        protected abstract void Write(VLogEntity entity);
        #endregion

        #region Private Fields
        static int configLevel = ConfigurationManager.AppSettings["VLogLevel"] == null
            ? 5
            : int.Parse(ConfigurationManager.AppSettings["VLogLevel"]);

        #endregion

        #region IVLog 成员

        public virtual void Debug(string msg)
        {
            if (configLevel >= (int)VLog.VLogLevel.Debug)
                this.Write(new VLogEntity
                {
                    ExceptionID = Guid.NewGuid().ToString(),
                    FullInfo = msg,
                    Level = VLogLevel.Debug
                });
        }

        public virtual void Info(string msg)
        {
            if (configLevel >= (int)VLog.VLogLevel.Info)
                this.Write(new VLogEntity
                {
                    ExceptionID = Guid.NewGuid().ToString(),
                    FullInfo = msg,
                    Level = VLogLevel.Info
                });
        }

        public virtual void Warn(string msg)
        {
            if (configLevel >= (int)VLog.VLogLevel.Warn)
                this.Write(new VLogEntity
                {
                    ExceptionID = Guid.NewGuid().ToString(),
                    FullInfo = msg,
                    Level = VLogLevel.Warn
                });
        }

        public virtual void Error(string msg)
        {
            if (configLevel >= (int)VLog.VLogLevel.Error)
                this.Write(new VLogEntity
                {
                    ExceptionID = Guid.NewGuid().ToString(),
                    FullInfo = msg,
                    Level = VLogLevel.Error
                });
        }

        public void Fatal(string msg)
        {
            if (configLevel >= (int)VLog.VLogLevel.Fatal)
                this.Write(new VLogEntity
                {
                    ExceptionID = Guid.NewGuid().ToString(),
                    FullInfo = msg,
                    Level = VLogLevel.Fatal
                });
        }

        #endregion

    }
}
